/*
 *   AS IS License
 * ==Naive Coders==
 */
package org.naive.metawatch.converter.cap;

import java.text.ParseException;
import org.naive.metawatch.converter.parser.FieldSize;
import org.naive.metawatch.converter.parser.Parsed;
import org.naive.metawatch.converter.parser.ParsedFixedSizeArray;
import org.naive.metawatch.converter.parser.ParsedInt;
import org.naive.metawatch.converter.parser.cap.CapParseContext;
import org.naive.metawatch.converter.parser.cap.CapParseable;

/**
 *
 * @author gbu.000
 */
public class CapClassDescriptorInfo extends CapParseable {

    @ParsedInt(FieldSize.BYTE)
    public byte token;
    @ParsedInt(FieldSize.BYTE)
    public byte access_flags;
    @Parsed(CapClassRef.class)
    public CapClassRef this_class_ref;
    @ParsedInt(FieldSize.BYTE)
    public byte interface_count;
    @ParsedInt(FieldSize.SHORT)
    public short field_count;
    @ParsedInt(FieldSize.SHORT)
    public short method_count;
//manual parsing
    public CapClassRef[] interfaces;
    public CapFieldDescriptorInfo[] fields;
    public CapMethodDescriptorInfo[] methods;

    @Override
    public void parse(CapParseContext px) throws ParseException {
        super.parse(px);
        try {
            interfaces = (CapClassRef[]) ParsedFixedSizeArray.FieldParser.parse(px,
                    u(interface_count), CapClassRef.class, CapClassRef.class);
            fields = (CapFieldDescriptorInfo[]) ParsedFixedSizeArray.FieldParser.parse(px,
                    u(field_count), CapFieldDescriptorInfo.class, CapFieldDescriptorInfo.class);
            methods = (CapMethodDescriptorInfo[]) ParsedFixedSizeArray.FieldParser.parse(px,
                    u(method_count), CapMethodDescriptorInfo.class, CapMethodDescriptorInfo.class);
        } catch (ParseException | InstantiationException | IllegalAccessException e) {
            ParseException exx = new ParseException("", px.getOffset());
            exx.initCause(e);
            throw exx;
        }
    }

    public boolean isPublic() {
        return 0 != (access_flags & 0x01);
    }
    public boolean isFinal() {
        return 0 != (access_flags & 0x10);
    }
    public boolean isInterface() {
        return 0 != (access_flags & 0x40);
    }
    public boolean isAbstract() {
        return 0 != (access_flags & 0x80);
    }
}
